package com.yyge.controller;

import com.yyge.dto.R;
import com.yyge.dto.ResultCode;
import com.yyge.entity.GoodsCategory;
import com.yyge.service.GoodsCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goodscategory")
public class GoodsCategoryController {
    // 注入业务逻辑层
    private GoodsCategoryService service;

    @Autowired/*自动装配*/
    public void setService(GoodsCategoryService goodscategoryService){
        this.service=goodscategoryService;
    }

    @GetMapping/* GET(SELECT): 查询 */
    public R get(){
        QueryWrapper<GoodsCategory> queryWrapper=new QueryWrapper<>();
        List<GoodsCategory> list =service.list(queryWrapper);
       return R.ok(list);
    }


    @PostMapping/* POST(CREATE): 新增&登陆 */
    public R post(@RequestBody GoodsCategory goodscategory){
        Boolean i=service.save(goodscategory);
        return i?R.ok("新增成功！"):R.error(ResultCode.ERROR);
    }

    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody GoodsCategory goodscategory){
        Boolean i=service.updateById(goodscategory);
        return i?R.ok("修改成功！"):R.error(ResultCode.ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody GoodsCategory goodscategory){
        return null;
    }

    @DeleteMapping/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@RequestBody GoodsCategory goodscategory){
        Boolean i=service.removeById(goodscategory.getCategoryId());
        return i?R.ok("删除成功！"):R.error(ResultCode.ERROR);
    }
}


